
let {mat4,vec3} = require('gl-matrix');


class GameItem {

    /**
     *
     * @param data =  {
        positions: posArray,
        normals: normalArray,
        texCoords: texCoordArray,
        indices: indicesArray,
    };
     * @param options
     */
    constructor(shaderInfo,data) {
        let gl = globalThis.app.gl;
        this.shaderInfo = shaderInfo;

        // vao
        let vao = this.vao = gl.createVertexArrayOES();
        gl.bindVertexArrayOES(vao);

        // posVBO
        let posVBO = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, posVBO);
        gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(data.positions),gl.STATIC_DRAW);
        gl.enableVertexAttribArray(shaderInfo.attribs['a_position']);
        gl.vertexAttribPointer(shaderInfo.attribs['a_position'],3,gl.FLOAT,false,0,0);

        // normalVBO
        let normalVBO = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER,normalVBO);
        gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(data.normals),gl.STATIC_DRAW);
        gl.enableVertexAttribArray(shaderInfo.attribs['a_normal']);
        gl.vertexAttribPointer(shaderInfo.attribs['a_normal'],3,gl.FLOAT,false,0,0);

        // indiceVBO
        let indiceVBO = gl.createBuffer();
        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,indiceVBO);
        gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,new Uint16Array(data.indices),gl.STATIC_DRAW);
        this.drawVertexCount = data.indices.length;
        gl.bindVertexArrayOES(null);

        //-----------------------------------
        // 包含基本的 T R S
        this.transMat = mat4.create();
        this.rotateMat = mat4.create();
        this.scaleMat = mat4.create();

        this.worldMatrix = mat4.create();
        this.lightMVP = mat4.create();

        this.color = vec3.fromValues(1,0,0);

        //-----
        // this.vao2 = gl.createVertexArrayOES();
        // gl.bindVertexArrayOES(this.vao2);
        // let vbo = gl.createBuffer();
        // gl.bindBuffer(gl.ARRAY_BUFFER,vbo);
        // gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(data.positions),gl.STATIC_DRAW);
        // gl.enableVertexAttribArray(0);
        // gl.vertexAttribPointer(0,3,gl.FLOAT,false,0,0);
        //
        // let idvbo = gl.createBuffer();
        // gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,idvbo);
        // gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,new Uint16Array(data.indices),gl.STATIC_DRAW);
        // gl.bindVertexArrayOES(null);
    }

    // render(camera,scene){
    //     let gl = globalThis.app.gl;
    //     let shaderInfo = this.shaderInfo;
    //
    //     gl.uniform3fv(shaderInfo.uniforms['u_color'],this.color);
    //
    //     mat4.mul(tmpMvMat,camera.viewMatrix,this.worldMatrix);
    //     gl.uniformMatrix4fv(shaderInfo.uniforms['viewMatrix'],false,camera.viewMatrix);
    //     gl.uniformMatrix4fv(shaderInfo.uniforms['mvMatrix'],false,tmpMvMat);
    //     gl.uniformMatrix4fv(shaderInfo.uniforms['nvMatrix'],false,tmpMvMat);
    //
    //     mat4.mul(lightMvp,scene.orhtorCamera.projMatrix,scene.orhtorCamera.viewMatrix);
    //     mat4.mul(lightMvp,lightMvp,this.worldMatrix);
    //     gl.uniformMatrix4fv(shaderInfo.uniforms['lightMVPMatrix'],false,lightMvp);
    //
    //     gl.bindVertexArrayOES(this.vao);
    //     gl.drawElements(gl.TRIANGLES,this.drawVertexCount,gl.UNSIGNED_SHORT,0);
    // }

    // depthPass(depShader,orthoCamera){
    //     let gl = globalThis.app.gl;
    //
    //     gl.useProgram(depShader.program);
    //     gl.uniformMatrix4fv(depShader.uniforms['projMatrix'],false,orthoCamera.projMatrix);
    //     mat4.mul(tmpMvMat,orthoCamera.viewMatrix,this.worldMatrix);
    //     gl.uniformMatrix4fv(depShader.uniforms['mvMatrix'],false,tmpMvMat);
    //     // gl.uniform3fv(depShader.uniforms['u_color'],this.color);
    //     gl.bindVertexArrayOES(this.vao);
    //     gl.drawElements(gl.TRIANGLES,this.drawVertexCount,gl.UNSIGNED_SHORT,0);
    // }

    updateWorldMatrixs(){
        mat4.mul(this.worldMatrix,this.transMat,this.rotateMat);
        mat4.mul(this.worldMatrix,this.worldMatrix,this.scaleMat);
    }
}

module.exports = GameItem;
